package com.wms.mail;

import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Mail {
	final static public String mailServer = "cnbjexm03.asia.corp.platform.com";
	
	private String username;
	private String password;
	
	private List<String> cc;
	private List<String> to;
	
	private MimeMessage message;
	private Session session;
	private InternetAddress from;
	
	public Mail(String username, String password) throws Exception{
		this.username = username;
		this.password = password;
		
		Properties props = new Properties ();
		props.put("mail.smtp.host", "smtp.platform.com");
		props.put("mail.smtp.auth","true");
		session=Session.getInstance(props);
		from=new InternetAddress("yongdu@platform.com");
	}
	
	public boolean authenticate() throws Exception{
		Transport transport = null;
		try{
			transport=session.getTransport("smtp");
			transport.connect(mailServer,username,password);
		}catch(Exception e){
			throw e;
		}finally{
			if(null != transport) transport.close();
		}
		return true;
	}
	
	public MimeMessage createMessage(String subject, String text) throws Exception{
		message=new MimeMessage(session);
		message.setFrom(from);
		message.setSubject(subject);
		message.setText(text);
		message.setSentDate(new Date());
		return message;
	}
	
	public MimeMessage addAttachment(String file) throws Exception{
		BodyPart mdp=new MimeBodyPart();
		FileDataSource fds=new FileDataSource(file);
		DataHandler dh=new DataHandler(fds);
		mdp.setFileName(file);
		mdp.setDataHandler(dh);
		Multipart mm=new MimeMultipart();
		mm.addBodyPart(mdp);
		message.setContent(mm);
		return message;
	}
	
	public void sendMail() throws Exception{
		message.saveChanges();
		if(null==to || 0==to.size()) throw new Exception("There must be at least one name in the To list.");
		InternetAddress[] toAddr=string2intAddr(to);
		message.setRecipients(Message.RecipientType.TO,toAddr);
		
		InternetAddress[] ccAddr;
		if(null!=cc && 0!=cc.size()){
			ccAddr=string2intAddr(cc);
			message.setRecipients(Message.RecipientType.CC,ccAddr);
		}
		
		Transport transport = null;
		try{
			transport=session.getTransport("smtp");
			transport.connect(mailServer,username,password);
			transport.sendMessage(message,message.getAllRecipients());
		}catch(Exception e){
			throw e;
		}finally{
			if(null != transport) transport.close();
		}
		
	}
	
	private InternetAddress[] string2intAddr(List<String> strs) throws Exception{
		InternetAddress[] addrs = new InternetAddress[strs.size()];
		for(int i=0;i<strs.size();i++){
			addrs[i] = new InternetAddress(strs.get(i));
		}
		return addrs;
	}

	public void setCc(List<String> cc) {
		this.cc = cc;
	}

	public List<String> getCc() {
		return cc;
	}

	public void setTo(List<String> to) {
		this.to = to;
	}

	public List<String> getTo() {
		return to;
	}
}
